//
//  LevelViewController.m
//  iOSPractice
//
//  Created by hoolai on 2018/11/27.
//  Copyright © 2018年 hoolai. All rights reserved.
//

#import "LevelViewController.h"

@interface LevelViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UIButton *backButton;
@property (strong, nonatomic) GameViewController *vcGame;

- (IBAction)backToPresentingView:(id)sender;
@end

@implementation LevelViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    UIStoryboard *sb=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
    self.vcGame = [sb instantiateViewControllerWithIdentifier:@"vcGame"];
}

- (void)viewDidAppear:(BOOL)animated{
    [self.collectionView reloadData];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return [AppSettings shareSettings].level;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ID = @"level";
    LevelCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    if(cell == nil){
        cell = [[[NSBundle mainBundle]loadNibNamed:@"LevelCell" owner:nil options:nil]lastObject];
    }
    //cell.label.text = [NSString stringWithFormat:@"%ld",indexPath.row];
    ((UILabel *)cell.contentView.subviews[0]).text = [NSString stringWithFormat:@"%ld",indexPath.row+1];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger level = indexPath.row + 1;
    self.vcGame.level = level; //传入点击的关卡
    [self presentViewController:self.vcGame animated:NO completion:nil];
    
}


- (IBAction)backToPresentingView:(id)sender {
    [self.presentingViewController dismissViewControllerAnimated:NO completion:nil];
}
@end
